package com.tianqu.tms.www.attn.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.SynAttnBo;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.attn.viewhelper.AttnViewhelper;

/**
 * @author zhangxueliang
 * 经办人修改结果controller
 */
public class AttnDetailUpdateResultController extends TmsController{
	private Logger logger = Logger.getLogger(AttnDetailUpdateResultController.class);
	private static final String SYSPATH = "sysPath";//
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			AttnViewhelper attnViewhelper = (AttnViewhelper)uc.getAttribute(TmsConstants.VIEWHELP);
			String sysPath = (String) uc.getAttribute(SYSPATH);
			//插入attn
			Attn attn = new Attn();
			Member member = Member.getInstance();
			TmsUserProfile profile = uc.getUserProfile();
			
			attn.setAttnId(attnViewhelper.getAttnId());
			attn.setAttnName(attnViewhelper.getAttnName());
			attn.setAttnAddress(attnViewhelper.getAttnAddress());	
			attn.setAttnIdCardNo(attnViewhelper.getAttnIdCardNo());			
			attn.setAttnPhone(attnViewhelper.getAttnPhone());
			attn.setMemberId(String.valueOf(member.getMemIdByMemName(attnViewhelper.getMemberId())));			
			attn.setUpdateUserId(profile.getUniqueId());
			attn.setAttnPhoto(attnViewhelper.getAttnPhoto());
			attn.updateAttn(attn, sysPath);
			//根据登录名更新用户真实姓名
			attn = attn.findAttnByAttnId(attnViewhelper.getAttnId());
			User user = new User();
			user.setLoginId(attn.getUserId());
			user.setRealName(attnViewhelper.getAttnName());
			user.setUpdateUserId(profile.getUniqueId());
			user.updateLoginInfoAtAttn(user);
			controller.commit();
			
			//微信同步
			MobileMessage message = new MobileMessage();
			message.putValue("attnId",attnViewhelper.getAttnId());
			message.setMessage("TMS经办人录入功能调用[经办人添加]接口失败,经办人ID为#attnId#");
			SynAttnBo api = new SynAttnBo();
			api.setMobileMessage(message);
			api.setAttnId(attnViewhelper.getAttnId());
			api.setMemberId(attn.getMemberId());
			api.setAttnName(attn.getAttnName());
			api.setAccessCardNo(attn.getAccessCardNo());
			api.setAttnPhone(attn.getAttnPhone());
			api.setUserId(attn.getUserId());
			PullingExecutor.getInstance().submit(api);
		} catch (Exception e)
		{
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}

}
